#!/bin/bash
# This script looks for all gpg files in a given directory (current_db).
# All files found are decrypted, passphrase must be in a file (pass_file).
# Then they are encrypted into a new location (newdb) with a new recipient (recipient).
# Relative paths from current_db down to each file are re-created in the new folder 
# (i.e. $current_db/mydir/file.gpg will be recreated under $newdb/mydir/file.gpg).


# Dir containing gpg files
current_db="$HOME/.priv"
# Dir where to create new files
newdb="$HOME/Desktop/newdb"
# File containig passphrase do decrypt
pass_file="$current_db/pass.txt"
# Recipient of newly encrypted messages
recipient="Marco Bridge"


cd $current_db;
mkdir -p "$newdb"

for f in $(find . -name "*.gpg"); do
    fpath=`dirname $f`
    fname=`basename $f`
    echo "PATH: $fpath, NAME: $fname"
    
    mkdir -p "$newdb/$fpath"
    
    gpg -d --output temp.txt --passphrase-file $pass_file --yes $f 
    gpg -e -r $recipient temp.txt
    mv temp.txt.gpg "$newdb/$fpath/$fname"
    echo "Created $newdb/$fpath/$fname"
    echo
done

echo "NON-GPG FILES (found in current_db):"
find . ! -name "*.gpg" -and ! -type d